﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using SystemCustomers.DBUtils;

namespace SystemCustomers
{
    public partial class ManageServiceToCustomer : Form
    {
        public ManageServiceToCustomer()
        {
            InitializeComponent();
            this.IsForSpecificCustomer = false;
            dateEnddate.Value = DateTime.Now.AddMonths(1);
            InitcombListCustomers();
            InitcombListServices();
        }

        #region Properties
        public ManageServiceToCustomer(string companyName,
            string serviceName, string companyID,
            string serviceID, DateTime startdate,
            DateTime enddate, string price, string priceCost,bool paid)
        {
            InitializeComponent();
            this.btnAdd.Visible = false;
            this.btnupdate.Visible = true;
            this.IsForSpecificCustomer = true;
            this.CompanyName = companyName;
            this.ServiceName = serviceName;
            this.CompanyId = companyID;
            this.ServiceId = serviceID;
            this.Startdate = startdate;
            this.Enddate = enddate;
            this.Price = price;
            this.PriceCost = priceCost;
            this.Paid = paid;
            InitcombListCustomers();
            InitcombListServices();
        }

        private bool canAdd = false;

        public bool IsForSpecificCustomer { get; set;}
      
        public string PriceCost
        {
            get { return txtbPriceCost.Text; }
            set { txtbPriceCost.Text = value; }
        }

        private string _companyName = string.Empty;

        public new string CompanyName
        {
            get { return _companyName = combListCust.SelectedItem.ToString(); }
            set { _companyName = value; }
        }

        private string _serviceName = string.Empty;

        public string ServiceName
        {
            get { return _serviceName = combListServices.SelectedItem.ToString(); }
            set { _serviceName = value; }
        }

        public string CompanyId { get; set;}
      

        public string ServiceId { get; set;}

        public DateTime Startdate
        {
            get { return dateStartdate.Value; }
            set { dateStartdate.Value = value; }
        }

        public DateTime Enddate
        {
            get { return dateEnddate.Value; }
            set { dateEnddate.Value = value; }
        }

        public string Price
        {
            get { return txtbPrice.Text; }
            set { txtbPrice.Text = value; }
        }

        public bool Paid
        {
            get { return cbPaid.Checked; }
            set { cbPaid.Checked = value; }
        }

        #endregion Properties

        #region Method

        private void EnableAdd()
        {
            btnAdd.Enabled = btnupdate.Enabled = IsValid() && canAdd;
        }

        private bool IsValid()
        {
            return (txtbPrice.Text.Trim() != string.Empty);
        }

        public void InitcombListCustomers()
        {
            if (IsForSpecificCustomer)
            {
                combListCust.Items.Clear();
                IDToName comp = new IDToName(this._companyName, this.CompanyId);
                combListCust.Items.Add(comp);
            }
            else
            {
                string idComp = string.Empty;
                string Compname = string.Empty;
                foreach (DataRow rd in SingletonManagerDb.Instance.DbViewingNameCustandIdCust().Rows)
                {
                    Compname = rd["companyName"].ToString();
                    idComp = rd["idCompany"].ToString();
                    IDToName company = new IDToName(Compname, idComp);
                    combListCust.Items.Add(company);
                }
            }
            if (combListCust.Items.Count > 0) combListCust.SelectedIndex = 0;
        }

        public void InitcombListServices()
        {
            if (IsForSpecificCustomer)
            {
                combListServices.Items.Clear();
                IDToName service = new IDToName(this._serviceName, this.ServiceId);
                combListServices.Items.Add(service);
            }
            else
            {
                foreach (DataRow rd in SingletonManagerDb.Instance.DbViewServiceNameandIdCust().Rows)
                {
                    string serviceName = rd["servicesName"].ToString();
                    string idService = rd["idService"].ToString();
                    IDToName service = new IDToName(serviceName, idService);
                    combListServices.Items.Add(service);
                }
            }

            if (combListServices.Items.Count > 0) combListServices.SelectedIndex = 0;
        }

        #endregion Method

        #region Event 
        private void combListCust_SelectedIndexChanged(object sender, EventArgs e)
        {
            IDToName company = (IDToName)this.combListCust.SelectedItem;
            if (company == null) return;
            string companyID = company.ID;
            this.CompanyId = companyID;
        }

        private void combListServices_SelectedIndexChanged(object sender, EventArgs e)
        {
            IDToName service = (IDToName)this.combListServices.SelectedItem;
            if (service == null) return;
            string serviceId = service.ID;
            this.ServiceId = serviceId;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var isAddToDataBase = (from DataRow rd in SingletonManagerDb.Instance.DbViewingServicesToCust().Rows
                                   let companyID = rd["idCompany"].ToString()
                                   let serviceID = rd["idService"].ToString()
                                    where CompanyId == companyID && ServiceId == serviceID
                                    select companyID).Any();
            if (!isAddToDataBase)
            {
                SingletonManagerDb.Instance.DbInsertManageCust(CompanyId, ServiceId, Startdate, Enddate, Price, PriceCost, CompanyName, ServiceName,Paid);
                MessageUtils.LogUtils.WriteToLog(String.Format(" Manage Customer To Services: {0} And {1}", CompanyName, ServiceName));
                MessageUtils.LogUtils.SystemEventLogsInformation(String.Format(" Manage Customer To Services: {0} And {1}", CompanyName, ServiceName));
                new MessageUtils.MessageBoxText("נוסף בהצלחה").OkMessaageBox();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageUtils.LogUtils.WriteToLog(String.Format(" Error Manage Customer To Services the company and service exist: {0} And {1}", CompanyName, ServiceName));
                MessageUtils.LogUtils.SystemEventLogsInformation(String.Format(" Error Manage Customer To Services the company and service exist: {0} And {1}", CompanyName, ServiceName));
                new MessageUtils.MessageBoxText("קיים במערכת אותו שרות ללקוח הנבחר").OkMessaageBox();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            MessageUtils.LogUtils.WriteToLog(" Exit From Manage Service To Customers");
            MessageUtils.LogUtils.SystemEventLogsInformation(" Exit From Manage Service To Customers");
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            SingletonManagerDb.Instance.DbUpdateServiceToCustomer(Startdate, Enddate, Price, PriceCost, CompanyId, ServiceId,Paid);
            MessageUtils.LogUtils.WriteToLog(" Update Manage Service To Customers");
            MessageUtils.LogUtils.SystemEventLogsInformation(" Update Manage Service To Customers");
            new MessageUtils.MessageBoxText("עודכן בהצלחה").OkMessaageBox();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }


        private void txtbPrice_TextChanged(object sender, EventArgs e)
        {
            if (!MyRegEx.Validate(txtbPrice.Text, MyRegEx.OnlyDigits))
            {
                epManageServiceToCustomer.SetError(txtbPrice, "רק ספרות");
                canAdd = false;
            }
            else
            {
                epManageServiceToCustomer.SetError(txtbPrice, string.Empty);
                canAdd = true;
            }
            EnableAdd();
        }

        private void txtbPrice_MouseHover(object sender, EventArgs e)
        {
            toolTipManageServiceToCustomer.SetToolTip(txtbPrice, "שדה חובה");
        }

        private void txtbPriceCost_TextChanged(object sender, EventArgs e)
        {
            if (!MyRegEx.Validate(txtbPriceCost.Text, MyRegEx.OnlyDigits) && txtbPriceCost.Text != string.Empty)
            {
                epManageServiceToCustomer.SetError(txtbPriceCost, "רק ספרות");
            }
            else
            {
                epManageServiceToCustomer.SetError(txtbPriceCost, string.Empty);
            }
        }

        private void txtbPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtbPriceCost_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }
        #endregion Event
    }

}
